﻿Public Class Cproforma

    Public Shared Function Añadir(ByVal descEmpresa As String, ByVal pPosCliente As EposibleCliente, ByVal pProformaCabecera As EproformaCabecera, _
                                  ByRef idProforma As Integer, ByVal pProformaDetalle As List(Of EproformaDetalle), ByVal pSimuladorCtas As List(Of EsimuladorCta)) As Boolean
        Dim resultado As Boolean = False

        Dim options As New TransactionOptions
        options.IsolationLevel = Transactions.IsolationLevel.ReadCommitted
        options.Timeout = New TimeSpan(0, 2, 0)

        Using scope As New TransactionScope(TransactionScopeOption.Required, options)
            Try
                Dim idPosCliente As Integer = 0
                Dim resultadoPosCliente As Boolean = RposibleCliente.Añadir(descEmpresa, pPosCliente, idPosCliente)
                pProformaCabecera.EposibleCliente.idPosCliente = idPosCliente
                Dim resultadoProforma As Boolean = RproformaCabecera.Añadir(descEmpresa, pProformaCabecera, idProforma)
                Dim resultadoProformaDetalle As Boolean = False
                For Each pd As EproformaDetalle In pProformaDetalle
                    pd.idProforma = idProforma
                    resultadoProformaDetalle = RproformaDetalle.Añadir(descEmpresa, pd)
                Next

                Dim resultadoProformaSimulador As Boolean = False
                If pSimuladorCtas IsNot Nothing Then
                    For Each ps As EsimuladorCta In pSimuladorCtas
                        ps.idProforma = idProforma
                        resultadoProformaSimulador = RsimuladorCta.Añadir(descEmpresa, ps)
                    Next
                End If
                If resultadoPosCliente AndAlso resultadoProforma AndAlso resultadoProformaDetalle Then resultado = True
                scope.Complete()
            Catch ex As Exception
                Throw
            End Try
        End Using

        Return resultado
    End Function
    Public Shared Function Añadir(ByVal descEmpresa As String, ByVal pPosCliente As EposibleCliente, ByVal pProformaCabecera As EproformaCabecera, _
                                  ByRef idProforma As Integer, ByVal pProformaDetalle As List(Of EproformaDetalle), ByVal pProformaDscto As List(Of EproformaDscto), _
                                  ByVal pSimuladorCtas As List(Of EsimuladorCta), ByVal pProformaAdicional As List(Of EproformaAdicional)) As Boolean
        Dim resultado As Boolean = False

        Dim options As New TransactionOptions
        options.IsolationLevel = Transactions.IsolationLevel.ReadCommitted
        options.Timeout = New TimeSpan(0, 2, 0)

        Using scope As New TransactionScope(TransactionScopeOption.Required, options)
            Try
                Dim idPosCliente As Integer = 0
                Dim resultadoPosCliente As Boolean = RposibleCliente.Añadir(descEmpresa, pPosCliente, idPosCliente)
                pProformaCabecera.EposibleCliente.idPosCliente = idPosCliente
                Dim resultadoProforma As Boolean = RproformaCabecera.Añadir(descEmpresa, pProformaCabecera, idProforma)
                Dim resultadoProformaDetalle As Boolean = False
                For Each pd As EproformaDetalle In pProformaDetalle
                    pd.idProforma = idProforma
                    resultadoProformaDetalle = RproformaDetalle.Añadir(descEmpresa, pd)
                Next

                Dim resultadoProformaDscto As Boolean = False
                If pProformaDscto IsNot Nothing Then
                    For Each pdscto As EproformaDscto In pProformaDscto
                        pdscto.idProforma = idProforma
                        resultadoProformaDscto = RproformaDscto.Añadir(descEmpresa, pdscto)
                    Next
                End If

                Dim resultadoProformaSimulador As Boolean = False
                If pSimuladorCtas IsNot Nothing Then
                    For Each ps As EsimuladorCta In pSimuladorCtas
                        ps.idProforma = idProforma
                        resultadoProformaSimulador = RsimuladorCta.Añadir(descEmpresa, ps)
                    Next
                End If

                Dim resultadoProformaAdicional As Boolean = False
                If pProformaAdicional IsNot Nothing Then
                    For Each pAdicional As EproformaAdicional In pProformaAdicional
                        pAdicional.idProforma = idProforma
                        resultadoProformaAdicional = RproformaAdicional.Añadir(descEmpresa, pAdicional)
                    Next
                End If
                If resultadoPosCliente AndAlso resultadoProforma AndAlso resultadoProformaDetalle Then resultado = True
                scope.Complete()
            Catch ex As Exception
                Throw
            End Try
        End Using

        Return resultado
    End Function
    Public Shared Function AñadirVersus(ByVal idEmpresa As Int32, ByVal descEmpresa As String, ByVal pPosCliente As EposibleCliente, ByVal pProformaCabecera As EproformaCabecera, _
                                        ByRef idProforma As Integer, ByVal pProformaDetalle As List(Of EproformaDetalle), ByVal pSimuladorCtas As List(Of EsimuladorCta)) As Boolean
        Dim resultado As Boolean = False

        Dim options As New TransactionOptions
        options.IsolationLevel = Transactions.IsolationLevel.ReadCommitted
        options.Timeout = New TimeSpan(0, 2, 0)

        Using scope As New TransactionScope(TransactionScopeOption.Required, options)
            Try
                Dim idPosCliente As Integer = 0
                Dim resultadoPosCliente As Boolean = RposibleCliente.Añadir(descEmpresa, pPosCliente, idPosCliente)
                pProformaCabecera.EposibleCliente.idPosCliente = idPosCliente
                Dim resultadoProforma As Boolean = RproformaCabecera.AñadirVersus(descEmpresa, pProformaCabecera, idProforma)
                Dim resultadoProformaDetalle As Boolean = False
                For Each pd As EproformaDetalle In pProformaDetalle
                    pd.idEmpresa = idEmpresa
                    pd.idProforma = idProforma
                    resultadoProformaDetalle = RproformaDetalle.AñadirVersus(descEmpresa, pd)
                Next

                Dim resultadoProformaSimulador As Boolean = False
                If pSimuladorCtas IsNot Nothing Then
                    For Each ps As EsimuladorCta In pSimuladorCtas
                        ps.idProforma = idProforma
                        resultadoProformaSimulador = RsimuladorCta.AñadirVersus(descEmpresa, ps)
                    Next
                End If
                If resultadoPosCliente AndAlso resultadoProforma AndAlso resultadoProformaDetalle Then resultado = True
                scope.Complete()
            Catch ex As Exception
                Throw
            End Try
        End Using

        Return resultado
    End Function
    Public Shared Function Editar(ByVal descEmpresa As String, ByVal pProformaCabecera As EproformaCabecera, ByRef idProforma As Integer) As Boolean
        Dim resultado As Boolean = False

        Dim options As New TransactionOptions
        options.IsolationLevel = Transactions.IsolationLevel.ReadCommitted
        options.Timeout = New TimeSpan(0, 2, 0)

        Using scope As New TransactionScope(TransactionScopeOption.Required, options)
            Try
                Dim resultadoProforma As Boolean = RproformaCabecera.Editar(descEmpresa, pProformaCabecera, idProforma)
                If resultadoProforma Then resultado = True
                scope.Complete()
            Catch ex As Exception
                Throw
            End Try
        End Using

        Return resultado
    End Function
    Public Shared Function EditarVersus(ByVal descEmpresa As String, ByVal pProformaCabecera As EproformaCabecera, ByRef idProforma As Integer) As Boolean
        Dim resultado As Boolean = False

        Dim options As New TransactionOptions
        options.IsolationLevel = Transactions.IsolationLevel.ReadCommitted
        options.Timeout = New TimeSpan(0, 2, 0)

        Using scope As New TransactionScope(TransactionScopeOption.Required, options)
            Try
                Dim resultadoProforma As Boolean = RproformaCabecera.EditarVersus(descEmpresa, pProformaCabecera, idProforma)
                If resultadoProforma Then resultado = True
                scope.Complete()
            Catch ex As Exception
                Throw
            End Try
        End Using

        Return resultado
    End Function

End Class
